package app

import (
	"chatgpt/model/base"
	"github.com/slclub/go-tips"
	"github.com/slclub/go-tips/json"
	"strconv"
)

type Product struct {
	ProcID        uint `gorm:"primaryKey;column:proc_id;autoIncrement"`
	ProcNo        string
	ProcName      string `gorm:"size:128;comment:商品名称"`
	Grounding     int    `gorm:"type:int(4);comment:1=上架，0=下架"`
	GroundingAt   int    `gorm:"type:int(10);comment:上架下架时间"`
	Num           int    `gorm:"type:int(10);comment:商品数量"`
	BenefitGood   string `gorm:"size:255;comment:权益数组"`
	Remark        string `gorm:"size:255;comment:备注"`
	Action        string // 作用 json
	ItemNo        string `gorm:"size:128;comment:平台商品号"`
	Images        string `gorm:"type:text;comment:图片"`
	PriceOrigin   int
	PriceDiscount int
	ActionMap     map[string]any
	base.ModelTime
}

func (self *Product) TableName() string {
	return "gpt_product"
}

func (self *Product) ConvFromMap(m map[string]any) {
	if val, ok := m["ProcID"]; ok && self.ProcID == 0 {
		self.ProcID = uint(tips.Any2Int64(val))
	}
	if val, ok := m["ProcNo"]; ok {
		self.ProcNo = tips.String(val)
	}
	if val, ok := m["ProcName"]; ok {
		self.ProcName = tips.String(val)
	}
	if val, ok := m["Grounding"]; ok {
		self.Grounding = tips.Int(val)
	}
	if val, ok := m["GroundingAt"]; ok {
		self.GroundingAt = tips.Int(val)
	}
	if val, ok := m["Num"]; ok {
		self.Num = tips.Int(val)
	}
	if val, ok := m["BenefitGood"]; ok {
		self.BenefitGood = tips.String(val)
	}
	if val, ok := m["Remark"]; ok {
		self.Remark = tips.String(val)
	}
	if val, ok := m["Action"]; ok {
		self.Action = tips.String(val)
	}
	if val, ok := m["ItemNo"]; ok {
		self.ItemNo = tips.String(val)
	}
	if val, ok := m["Images"]; ok {
		self.Images = tips.String(val)
	}
	self.ModelTime.ConvToMap(m)
}

func (self *Product) ConvToMap() map[string]any {
	rtn := map[string]any{
		"ProcID":        self.ProcID,
		"ProcNo":        self.ProcNo,
		"ProcName":      self.ProcName,
		"Grounding":     self.Grounding,
		"GroundingAt":   self.GroundingAt,
		"Num":           self.Num,
		"BenefitGood":   self.BenefitGood,
		"Remark":        self.Remark,
		"Action":        self.Action,
		"PriceDiscount": self.PriceDiscount,
		"ItemNo":        self.ItemNo,
		"Images":        self.Images,
		"Cost":          strconv.FormatFloat(float64(self.PriceDiscount)/100, 'f', -1, 64) + "¥",
	}
	self.ModelTime.ConvToMap(rtn)
	return rtn
}

func (self *Product) DecodeAction() {
	switch {
	case self.Action != "":
		self.ActionMap = map[string]any{}
		json.Unmarshal([]byte(self.Action), &self.ActionMap)
	}
}

// benefit good for product
type GoodItem struct {
	ID    int
	Value string
}
